package library.client;

import java.util.HashSet;

import javax.naming.Context;
import javax.naming.InitialContext;

import library.IF.BookDAO;
import library.IF.CustomerDAO;
import library.IF.LibraryManagerIF;

import org.jboss.security.client.SecurityClient;
import org.jboss.security.client.SecurityClientFactory;

public class Main {
	public static void main(String[] args) throws Exception {
		Context ctx = new InitialContext();

		SecurityClient securityClient = SecurityClientFactory.getSecurityClient();

		securityClient.setSimple("fred", "geheim");
		securityClient.login();

		LibraryManagerIF manager = (LibraryManagerIF) ctx.lookup("LibraryManagerBean/remote");
		System.out.println("librarymanager=" + manager);

		printBooks(manager);
		printCustomers(manager);
		manager.addCredits("Eric", 1);
		printCustomers(manager);
		securityClient.logout();
	}

	private static void printBooks(LibraryManagerIF manager) {
		/*
		HashSet<BookDAO> books = manager.allBooks();

		for (BookDAO book : books) {
			System.out.println(book.getTitle());
		}

		System.out.println("----------------------------------------------");
		*/
	}

	private static void printCustomers(LibraryManagerIF manager) {
		HashSet<CustomerDAO> customers = manager.allCustomers();

		for (CustomerDAO customer : customers) {
			System.out.print(customer.getName() + ", " + customer.getCredits());

			HashSet<String> books = customer.getBooks();

			for (String title : books) {
				System.out.print(", " + title);
			}

			System.out.println();
		}

		System.out.println("----------------------------------------------");
	}
}
